package vn.aptech.leaveManagementSystem.view.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import vn.aptech.leaveManagementSystem.business.dto.Application;
import vn.aptech.leaveManagementSystem.business.dto.Detail;
import vn.aptech.leaveManagementSystem.business.dto.Employee;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;
import vn.aptech.leaveManagementSystem.dao.DBManager;

import com.toedter.calendar.JDateChooser;
import javax.swing.JPasswordField;

public class AddUser extends JFrame{

	DBManager con = new DBManager();
    Connection conn ;
    String user ;
    String pos;
    String sql;   
    String exist;
    PreparedStatement ps;
	private JTextField txtFirstname;
	private JTextField txtLastname;
	JDateChooser txtBirthday;
	private JTextField txt1;
	private JTextArea txtAddress;
	private JScrollPane scrollpane;
	private JTextField txtEmail;
	private JTextField txtPhone;
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JPasswordField txtConfirmPassword;
	JFrame thisFrame = this;
	MyModel mymodel;
	public AddUser() {
		
            try {
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
       
		
		setResizable(false);
		// TODO Auto-generated constructor stub

		
		mymodel=new MyModel();
		
		JPanel pnlMain = new JPanel();
		pnlMain.setLayout(null);
		
		getContentPane().add(pnlMain);
		
		JLabel lblFirstname = new JLabel("Firstname");
		lblFirstname.setBounds(22, 68, 76, 14);
		pnlMain.add(lblFirstname);
		
		JLabel lblLastname = new JLabel("Lastname");
		lblLastname.setBounds(22, 97, 65, 14);
		pnlMain.add(lblLastname);
		
		JLabel lblGender = new JLabel("Gender");
		lblGender.setBounds(22, 128, 46, 14);
		pnlMain.add(lblGender);
		
		JLabel lblBirthday = new JLabel("Birthday");
		lblBirthday.setBounds(22, 158, 65, 14);
		pnlMain.add(lblBirthday);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(22, 210, 76, 14);
		pnlMain.add(lblAddress);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(22, 322, 46, 14);
		pnlMain.add(lblEmail);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(22, 350, 46, 14);
		pnlMain.add(lblPhone);
		
		JLabel lblDepartmentid = new JLabel("Department");
		lblDepartmentid.setBounds(22, 267, 76, 14);
		pnlMain.add(lblDepartmentid);
		
		JLabel lblPositionid = new JLabel("Position");
		lblPositionid.setBounds(22, 294, 65, 14);
		pnlMain.add(lblPositionid);
		
		JLabel lblJoindate = new JLabel("JoinDate");
		lblJoindate.setBounds(22, 381, 76, 14);
		pnlMain.add(lblJoindate);
		
		JLabel lblUsername = new JLabel("UserName");
		lblUsername.setBounds(22, 406, 76, 14);
		pnlMain.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(22, 434, 76, 14);
		pnlMain.add(lblPassword);
		
		JLabel lblRpassword = new JLabel("Confirm Password");
		lblRpassword.setBounds(22, 459, 116, 24);
		pnlMain.add(lblRpassword);
		
		txtFirstname = new JTextField();
		txtFirstname.setBounds(137, 65, 224, 20);
		pnlMain.add(txtFirstname);
		txtFirstname.setColumns(10);
		
		txtLastname = new JTextField();
		txtLastname.setColumns(10);
		txtLastname.setBounds(137, 94, 224, 20);
		pnlMain.add(txtLastname);
		
		String strGender[] = {"Male","Female"};
		
		final JComboBox cbbGender = new JComboBox(strGender);
		cbbGender.setBounds(138, 125, 76, 20);
		pnlMain.add(cbbGender);
		
	
		
		txtBirthday =  new JDateChooser();
		txtBirthday.setBounds(137, 152, 224, 20);
		pnlMain.add(txtBirthday);
		
		final JDateChooser txtJoindate = new JDateChooser();
		txtJoindate.setBounds(137, 375, 224, 20);
		pnlMain.add(txtJoindate);
		
		txtAddress = new JTextArea();
		txtAddress.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane(txtAddress);
		scrollPane.setBounds(137, 178, 207, 79);
		//scrollPane.add();
		pnlMain.add(scrollPane);
		///////////////////////////////////////////////////////////////
		
		String strDepartment[] = {"Business Department","Technical Department","HR Department","Accounting","Sale","Marketing"};
		final JComboBox cbbDepartment = new JComboBox(strDepartment);
		cbbDepartment.setBounds(137, 264, 123, 20);
		pnlMain.add(cbbDepartment);
		
		/////////////////////////////////////////////////////////////
		String strPosition[] = {"Admin","Director","Manager","Employee"};
		
		final JComboBox cbbPosition = new JComboBox(strPosition);
		cbbPosition.setBounds(137, 291, 121, 20);
		pnlMain.add(cbbPosition);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(137, 319, 224, 20);
		pnlMain.add(txtEmail);
		txtEmail.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(137, 347, 121, 20);
		pnlMain.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(137, 403, 224, 20);
		pnlMain.add(txtUsername);
		txtUsername.setColumns(10);
		
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					//open connection
					conn = con.getConn();
					String firstname = txtFirstname.getText().trim();
					String lastname = txtLastname.getText().trim();
					String gender = cbbGender.getSelectedItem().toString(); 
					//Date birthday = (Date)txtBirthday.getDate();
					Date birthday =  MyParse.toSqlDate(txtBirthday.getDate());
					String address = txtAddress.getText().trim();
					//((JTextField)txtJoindate.getDateEditor().getUiComponent()).getText();
					String department = cbbDepartment.getSelectedItem().toString();
					String position = cbbPosition.getSelectedItem().toString();
					String email = txtEmail.getText().trim();
					String phone = txtPhone.getText().trim();
					//Date joindate = (Date) txtJoindate.getDate();
					Date joindate = MyParse.toSqlDate(txtJoindate.getDate());
					String username = txtUsername.getText().trim();
					char[] pwd = txtPassword.getPassword();
					String password = new String(pwd).trim();
					char[] cpwd = txtConfirmPassword.getPassword();
					String confirmpassword = new String(cpwd);
					
					
					
					
					int departmentID = 1;
		            
		            if(department.equals("Technical Department"))
		            {
		                departmentID = 2;    
		            }
		            else if(department.equals("HR Department"))
		            {
		                departmentID = 3;
		            }
		           /* else if(department.equals("Accounting"))
		            {
		                departmentID = 4;
		            }
		            else if(department.equals("Sale"))
		            {
		                departmentID = 5;
		            }
		            else if(department.equals("Marketing"))
		            {
		                departmentID = 6;
		            }*/
		            
		            
		            int positionID = 1;
		            
		            if(position.equals("Director"))
		            {
		                positionID = 2;
		            }
		            else if(position.equals("Manager"))
		            {
		                positionID = 3;
		            }
		            else if(position.equals("Employee"))
		            {
		                positionID = 4;
		            }
		            		           
		            
		            if(firstname.equals(""))
		            {
		                JOptionPane.showInputDialog(thisFrame, "Please enter firstname");   
		            }
		            else if(lastname.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Lastname");
		            }   
		            else if(birthday.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Birthday"); 
		            }
		            else if(address.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Address"); 
		            }
		            else if(email.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Email"); 
		            }
		            else if(!email.matches("[a-zA-z]+[a-zA-Z0-9]*@[a-zA-Z]+[.][a-zA-Z]+"))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Email is not correct, Please try again !");
		            }
		            else if(phone.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Number Phone"); 
		            }    
		            else if(joindate.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Joindate"); 
		            }
		            else if(username.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Username"); 
		            }
		            else if(password.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Password"); 
		            }
		            else if(!password.matches("[a-zA-Z0-9]{6,12}"))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Password is not correct, Please try again !");
		            }
		            else if(confirmpassword.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Confirm Password"); 
		            }
		            else if(!confirmpassword.matches("[a-zA-Z0-9]{6,12}"))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Confirm Password is not correct, Please try again !");
		            } 
		            else if(!confirmpassword.equals(password))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please confirm password correctly");
		            }
		            
		            
		            //ngay cho nay con time thi bat loi tiep
		            Employee emp = new Employee();
		            emp.setFirstName(firstname);
		            emp.setLastName(lastname);
		            if (gender == "Male") {
		            	emp.setGender(true);
		            }else emp.setGender(false);
		            emp.setBirthday(birthday);
					emp.setAddress(address);
		            emp.setDepartmentID(departmentID);
		            emp.setPositionID(positionID);
					emp.setEmail(email);
					emp.setPhone(phone);
					emp.setJoinDate(joindate);
					emp.setUserName(username);
					emp.setPassword(password);
					
					
					MyModel mymodel = new MyModel();
					int row = mymodel.employees.addEmployee(emp); //mymodel.employees.addEmployee(emp) tra ve return cs.executeUpdate();
					if (row != 0) {
						JOptionPane.showMessageDialog(thisFrame, "Create Account suseccful");
						thisFrame.setVisible(false);
						AdminForm admin = new AdminForm();
					}
					 else
                     {
						 JOptionPane.showMessageDialog(thisFrame, "Create Account Unsuccessful, Please try again");
						 txtPassword.setText("");
                         txtConfirmPassword.setText("");
                     }
					//bat app tai day
					/*ResultSet rs=mymodel.employees.getEmployee(1);
					Employee app=MyParse.toEmployee(rs);
					app.setFirstName("sdfsd");
					mymodel.employees.updateEmployee(app);
					*/
					//String d1  = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();  
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnOk.setBounds(68, 507, 89, 23);
		btnOk.setIcon(new ImageIcon("icons/gif/16x16/OK.gif"));
		pnlMain.add(btnOk);
		
		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtFirstname.setText("");
				txtLastname.setText("");
				cbbGender.setSelectedIndex(0);
				txtBirthday.setDate(null);
				txtAddress.setText("");
				cbbDepartment.setSelectedIndex(0);
				cbbPosition.setSelectedIndex(0);
				txtEmail.setText("");
				txtPhone.setText("");
				txtJoindate.setDate(null);
				txtUsername.setText("");
				txtPassword.setText("");
				txtConfirmPassword.setText("");
			}
		});
		btnReset.setBounds(167, 507, 89, 23);
		btnReset.setIcon(new ImageIcon("icons/gif/16x16/Redo.gif"));
		pnlMain.add(btnReset);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminForm adminForm =  new AdminForm();
				adminForm.setVisible(true);
			}
		});
		btnCancel.setBounds(266, 507, 89, 23);
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		pnlMain.add(btnCancel);
		
		
		
		
		//txtAddress.setBounds(137, 179, 205, 77);
		//pnlMain.add(txtAddress);
		//txtAddress.setBounds(114, 178, 207, 79);
		//txtAddress.setColumns(10);
		
//		scrollpane = new JScrollPane(txtAddress);
//		scrollpane.setBounds(137, 179, 205, 77);
//		pnlMain.add(scrollpane);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(137, 431, 224, 20);
		pnlMain.add(txtPassword);
		
		txtConfirmPassword = new JPasswordField();
		txtConfirmPassword.setBounds(137, 461, 224, 20);
		pnlMain.add(txtConfirmPassword);
		//txtBirthday.setColumns(10);
		setVisible(true);
		setTitle("Add User");
		setSize(390, 569);
		setLocationRelativeTo(null);
	}
}
